package cn.itsource.pethome.system.shop.service.impl;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.system.employee.domain.Employee;
import cn.itsource.pethome.system.employee.mapper.EmployeeMapper;
import cn.itsource.pethome.system.logininfo.domain.Logininfo;
import cn.itsource.pethome.system.logininfo.mapper.LogininfoMapper;
import cn.itsource.pethome.system.shop.domain.Shop;
import cn.itsource.pethome.system.shop.domain.ShopRo;
import cn.itsource.pethome.system.shop.mapper.ShopMapper;
import cn.itsource.pethome.system.shop.query.ShopQuery;
import cn.itsource.pethome.system.shop.service.IShopService;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.utils.AuthService;
import cn.itsource.pethome.utils.BusinessException;
import cn.itsource.pethome.utils.MD5Utils;
import cn.itsource.pethome.utils.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2023-01-07
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private AuthService authService;
    @Resource
    private LogininfoMapper logininfoMapper;

    @Override
    public void add(Shop shop) {
        //新增的判断
        if(shop.getName() == null)
            throw new BusinessException("店铺名不能为空！");

        //对接百度智能文本审核
        Boolean conclusion = authService.getConclusion(shop.getName());
        if (!conclusion)   //审核结果不合规就抛出异常
            throw new BusinessException("注册失败，您的店铺名不合规！");

        //判断店铺名是否重复
        ShopQuery shopQuery = new ShopQuery();
        shopQuery.setName(shop.getName());
        Long aLong = shopMapper.countPage(shopQuery);
        if (aLong != 0)
            throw new BusinessException("店铺名【"+shop.getName()+"】已存在");

        //其他判断非空
        if(shop.getAddress() == null)
            throw new BusinessException("店铺地址不能为空！");
        if(shop.getTel() == null)
            throw new BusinessException("联系方式不能为空！");
        //补充数据  创建注册时间，审核状态
        shop.setRegisterTime(new Date());
        shop.setState(BaseConstants.Shop.STATE_WAIT_AUTID);

        //新增管理员，得到管理员id，添加到店铺中的管理员id，新增店铺，得到店铺id修改进管理员表
        Employee admin = shop.getAdmin();//获取员工信息
        employeeMapper.add(admin);
        shop.setAdminId(admin.getId());
        shopMapper.add(shop);
        admin.setShop_id(shop.getId());  //设置店铺id到员工表，使两表产生关联

        //同步员工信息到logininfo表
        String salt = StrUtils.getComplexRandomString(32);
        admin.setSalt(salt);
        admin.setPassword(MD5Utils.encrypByMd5(admin.getPassword()+salt));

        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(BaseConstants.Login.TYPE_0);//设置账户类型为管理员
        logininfo.setDisable(BaseConstants.Login.DISABLE_0);//先设置为不可用，用户在邮箱激活以后，才可用
        logininfoMapper.add(logininfo);
        admin.setLogininfo_id(logininfo.getId());
        employeeMapper.update(admin);   //更新员工表
    }

    //店铺激活
    @Override
    public void update(Shop shop) {
        shop.setState(BaseConstants.Shop.STATE_ACTIVE_SUCCESS);//设置店铺状态为激活成功
        Employee one = employeeMapper.findOne(shop.getAdminId());
        one.setState(BaseConstants.Employee.STATE_NORMAL);//设置对应员工账号状态为正常
        Logininfo logininfo = logininfoMapper.findOne(one.getLogininfo_id());
        logininfo.setDisable(BaseConstants.Login.DISABLE_1);//设置logininfo表中对应账号信息为可用
        logininfoMapper.update(logininfo);
        employeeMapper.update(one);
        super.update(shop);
    }

    //图表数据
    @Override
    public List<ShopRo> countByDate() {
        return shopMapper.countByDate();
    }
}
